﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace testProject.MVVMHelpers
{
    public static class MyTest
    {
        //private static readonly List<FrameworkElement> _wiredUpElements = new List<FrameworkElement>();
        //private static readonly object _wiredUpElementsLock = new object();

        public static readonly DependencyProperty testProperty
            = DependencyProperty.RegisterAttached("Test",
                typeof(object),
                typeof(MyTest),
                new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, MouseOverBindingPropertyChanged));

        public static readonly DependencyProperty TestParameterProperty =
            DependencyProperty.RegisterAttached("TestParameter", typeof(object), typeof(MyTest), new PropertyMetadata(null));
        public static void SetTest(FrameworkElement element, object value)
        {
            element.SetValue(testProperty, value);
        }

        public static void SetTestParameter(FrameworkElement element, object value)
        {
            element.SetValue(TestParameterProperty, value);
        }

        private static void MouseOverBindingPropertyChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            var element = d as FrameworkElement;
            //if (element == null || _wiredUpElements.Contains(element))
            //{
            //    return;
            //}

            //lock (_wiredUpElementsLock)
            //{
            //    if (_wiredUpElements.Contains(element))
            //    {
            //        return;
            //    }

            //    _wiredUpElements.Add(element);
                element.MouseEnter += (sender, args) =>
                    {
                        var s = (FrameworkElement)sender;
                        var parameter = ((FrameworkElement)sender).GetValue(TestParameterProperty);

                        (s.GetValue(testProperty) as ICommand).Execute(parameter);
                    };
            //}
        }
    }
}
